﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Windows.Forms;

namespace IWebApp2
{
    public partial class SignIn : System.Web.UI.Page
    {
        //instantiate an object for JabberClient protocols
        public static jabber.client.JabberClient jabberClient1;
       
   
        protected void Page_Load(object sender, EventArgs e)
        {
            //codes within !IsPostBack will execute once only
            if (!IsPostBack)
            {
                jabberClient1 = new jabber.client.JabberClient();
                jabberClient1.OnConnect += new jabber.connection.StanzaStreamHandler(jabberClient1_OnConnect);
                jabberClient1.OnAuthError += new jabber.protocol.ProtocolHandler(jabberClient1_OnAuthError);
               
            }
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            //set jabber ID
            jabberClient1.User = this.txtUserID.Text;
            
            //set jabber id to myID
            IWebApp2.IMForm.myID = this.txtUserID.Text;
            
            //set password
            jabberClient1.Password = this.txtPassword.Text;
            //ser server name
            jabberClient1.Server = "yb.yehba.com";
            //set port value
            jabberClient1.Port = 8222;
            //invoke method to connect
            jabberClient1.Connect();

            //wait until authenticated
            while (true)
            {
                if (jabberClient1.IsAuthenticated)
                {
                    //if authenticated proceed with IM GUI
                    Response.Redirect("IMForm.aspx");
                    break;
                }
            }
        }

        /// <summary>
        /// Event handler that returns jabber authentication error
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="x"></param>
        protected void jabberClient1_OnAuthError(object sender, System.Xml.XmlElement x)
        {
            //MessageBox.Show("Error Login");
        }

       /// <summary>
       /// Event handler that signals disconnection 
       /// </summary>
       /// <param name="sender"></param>
        protected void jabberClient1_OnDisconnect(object sender)
        {
            //MessageBox.Show("OnDisconnect");
        }

     /// <summary>
     /// OnConnect to jabber server 
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="stanza"></param>
        protected void jabberClient1_OnConnect(object sender, jabber.connection.StanzaStream stanza)
        {
            //MessageBox.Show("Connect");
            jabberClient1.OnAuthenticate += new bedrock.ObjectHandler(jabberClient1_OnAuthenticate);
        }

      /// <summary>
      /// On Authentication
      /// </summary>
      /// <param name="sender"></param>
        protected void jabberClient1_OnAuthenticate(object sender)
        {
            //MessageBox.Show("Authenticated");
        }
      

    }
}



/*
  
 * 
 * 
 * 
    
    
    <asp:ImageButton ID="ImageButton1" runat="server" Height="5%" 
        ImageUrl="~/Images/mike.PNG" onclick="ImageButton1_Click" Width="73%" />
    <asp:ImageButton ID="ImageButton2" runat="server" Height="5%" 
        ImageUrl="~/Images/btn_blue_right_cancel.png" onclick="ImageButton2_Click" 
        style="margin-left: -6px" Width="20%" />
*/